/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVK;

import DAO.CTPNhapKhoDao;
import DAO.NhaCungCapDao;
import DAO.PNhapKhoDao;
import DAO.SanPhamDao;
import GUI.Var;
import POJO.CTPNhapKho;
import POJO.NhaCungCap;
import POJO.PNhapKho;
import POJO.SanPham;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Khoi
 */
public class frLapPhieuNhapKho extends javax.swing.JInternalFrame {

	/**
	 * Creates new form frLapPhieuNhapKho
	 */
	private DefaultComboBoxModel cbModel;
	private DefaultComboBoxModel cbModel2;
	private DefaultTableModel tbModel;
	private boolean load;
	private boolean save;
	private ArrayList<CTPNhapKho> dsCT = new ArrayList<CTPNhapKho>();

	private void loadCobo() {

		NhaCungCapDao dao = new NhaCungCapDao();

		ArrayList<NhaCungCap> ds = dao.layDanhSach();
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel.addElement(ds.get(i));
		}
		if (cbModel.getSize() > 0) {
			cbModel.setSelectedItem(cbModel.getElementAt(0));
			loadCobo2();
		}

	}

	private void loadCobo2() {
		this.cbModel2 = new DefaultComboBoxModel();
		this.cbSanPham.setModel(cbModel2);

		try {
			Object ob = cbModel.getSelectedItem();
			NhaCungCap ncc = (NhaCungCap) ob;
			SanPhamDao d = new SanPhamDao();

			ArrayList<SanPham> ds = d.layDanhSachByMaNhaCC(ncc.getMaNCC());
			for (int i = 0; i < ds.size(); i++) {
				this.cbModel2.addElement(ds.get(i));
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Lỗi", "Thông báo", JOptionPane.INFORMATION_MESSAGE);

		}


	}

	public frLapPhieuNhapKho() {
		initComponents();
		save = false;
		load = false;
		Var TM = new Var();
		txTenNV.setText(TM.NV.getTenNV());

		int ng, t, n;
		ng = Var.NGAY.get(Calendar.DATE);
		t = Var.NGAY.get(Calendar.MONTH);
		n = Var.NGAY.get(Calendar.YEAR);

		String stn;
		stn = ng + "-" + (t + 1) + "-" + n;
		txNgay.setText(stn);


		this.cbModel = new DefaultComboBoxModel();
		this.cbModel2 = new DefaultComboBoxModel();
		this.tbModel = new DefaultTableModel();

		this.cbNhaCC.setModel(cbModel);
		this.cbSanPham.setModel(cbModel2);

		loadCobo();
		load = true;
		DefaultFrom();
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jPanel2 = new javax.swing.JPanel();
              jLabel2 = new javax.swing.JLabel();
              txTenNV = new javax.swing.JTextField();
              jLabel4 = new javax.swing.JLabel();
              txNgay = new javax.swing.JTextField();
              jLabel5 = new javax.swing.JLabel();
              cbNhaCC = new javax.swing.JComboBox();
              jPanel3 = new javax.swing.JPanel();
              jPanel4 = new javax.swing.JPanel();
              cbSanPham = new javax.swing.JComboBox();
              jLabel6 = new javax.swing.JLabel();
              jLabel7 = new javax.swing.JLabel();
              txSoLuong = new javax.swing.JTextField();
              jLabel8 = new javax.swing.JLabel();
              txGia = new javax.swing.JTextField();
              jButton1 = new javax.swing.JButton();
              jButton3 = new javax.swing.JButton();
              jPanel6 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              tbChiTiet = new javax.swing.JTable();
              jButton2 = new javax.swing.JButton();
              jLabel3 = new javax.swing.JLabel();
              txMaPN = new javax.swing.JTextField();
              jButton4 = new javax.swing.JButton();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("LẬP PHIẾU NHẬP KHO (mua thêm sản phẩm)");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jLabel1)
                            .addGap(0, 0, 0))
              );

              jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn nhà cung cấp"));

              jLabel2.setText("Nhân viên:");

              txTenNV.setEnabled(false);
              txTenNV.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txTenNVActionPerformed(evt);
                     }
              });

              jLabel4.setText("Ngày nhập:");

              txNgay.setEnabled(false);
              txNgay.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txNgayActionPerformed(evt);
                     }
              });
              txNgay.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            txNgayInputMethodTextChanged(evt);
                     }
              });
              txNgay.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            txNgayKeyReleased(evt);
                     }
              });
              txNgay.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            txNgayVetoableChange(evt);
                     }
              });

              jLabel5.setText("Nhà cung cấp:");

              cbNhaCC.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
              cbNhaCC.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cbNhaCCActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(90, 90, 90)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel5)
                                   .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel2Layout.createSequentialGroup()
                                          .addComponent(txTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(jLabel4)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(txNgay, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                   .addComponent(cbNhaCC, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(136, Short.MAX_VALUE))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(txTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel2)
                                   .addComponent(txNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel5)
                                   .addComponent(cbNhaCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Lập phiếu"));

              jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn sản phẩm"));
              jPanel4.setToolTipText("");

              cbSanPham.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
              cbSanPham.setToolTipText("Nhà cung cấp có thể cung cấp sản phẩm này");
              cbSanPham.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cbSanPhamActionPerformed(evt);
                     }
              });

              jLabel6.setText("Sản phẩm:");

              jLabel7.setText("Số lượng:");

              txSoLuong.setText("1");
              txSoLuong.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txSoLuongActionPerformed(evt);
                     }
              });
              txSoLuong.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            txSoLuongInputMethodTextChanged(evt);
                     }
              });
              txSoLuong.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            txSoLuongKeyReleased(evt);
                     }
              });
              txSoLuong.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            txSoLuongVetoableChange(evt);
                     }
              });

              jLabel8.setText("Giá:");

              txGia.setText("0");
              txGia.setToolTipText("Giá tham khảo đang là giá bán hiện hành của sản phẩm");
              txGia.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txGiaActionPerformed(evt);
                     }
              });
              txGia.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            txGiaInputMethodTextChanged(evt);
                     }
              });
              txGia.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            txGiaKeyReleased(evt);
                     }
              });
              txGia.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            txGiaVetoableChange(evt);
                     }
              });

              jButton1.setText("+");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                     }
              });

              jButton3.setText("-");
              jButton3.setToolTipText("Xóa chi tiết phía dưới");
              jButton3.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton3ActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGap(48, 48, 48)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addComponent(jLabel6)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(cbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(jLabel7)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(txSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(jLabel8)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(txGia, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addGap(0, 0, Short.MAX_VALUE)
                                          .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addGap(137, 137, 137))))
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel6)
                                   .addComponent(cbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel7)
                                   .addComponent(txSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(txGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel8))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jButton1)
                                   .addComponent(jButton3))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              tbChiTiet.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã sản phẩm", "Tên", "Số lượng", "Giá nhập"
                     }
              ));
              jScrollPane1.setViewportView(tbChiTiet);

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1)
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
              );

              jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jButton2.setText("Lập phiếu nhập kho");
              jButton2.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton2ActionPerformed(evt);
                     }
              });

              jLabel3.setText("Mã phiếu nhập:");

              txMaPN.setEnabled(false);
              txMaPN.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txMaPNActionPerformed(evt);
                     }
              });
              txMaPN.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            txMaPNInputMethodTextChanged(evt);
                     }
              });
              txMaPN.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            txMaPNKeyReleased(evt);
                     }
              });
              txMaPN.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            txMaPNVetoableChange(evt);
                     }
              });

              jButton4.setText("Phiếu nhập kho mới");
              jButton4.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton4ActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2)
                            .addGap(165, 165, 165))
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addContainerGap())
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGap(64, 64, 64)
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txMaPN, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(txMaPN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel3))
                            .addGap(8, 8, 8)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jButton2)
                                   .addComponent(jButton4))
                            .addGap(0, 0, 0))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

       private void txMaPNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txMaPNActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txMaPNActionPerformed

       private void txMaPNInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txMaPNInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_txMaPNInputMethodTextChanged

       private void txMaPNKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txMaPNKeyReleased
		// TODO add your handling code here:
//              vbtSua();
       }//GEN-LAST:event_txMaPNKeyReleased

       private void txMaPNVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_txMaPNVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_txMaPNVetoableChange

       private void txNgayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txNgayActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txNgayActionPerformed

       private void txNgayInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txNgayInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_txNgayInputMethodTextChanged

       private void txNgayKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txNgayKeyReleased
		// TODO add your handling code here:
       }//GEN-LAST:event_txNgayKeyReleased

       private void txNgayVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_txNgayVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_txNgayVetoableChange

       private void txSoLuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txSoLuongActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txSoLuongActionPerformed

       private void txSoLuongInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txSoLuongInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_txSoLuongInputMethodTextChanged

       private void txSoLuongKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txSoLuongKeyReleased
		// TODO add your handling code here:
       }//GEN-LAST:event_txSoLuongKeyReleased

       private void txSoLuongVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_txSoLuongVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_txSoLuongVetoableChange

       private void txGiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txGiaActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txGiaActionPerformed

       private void txGiaInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txGiaInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_txGiaInputMethodTextChanged

       private void txGiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txGiaKeyReleased
		// TODO add your handling code here:
       }//GEN-LAST:event_txGiaKeyReleased

       private void txGiaVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_txGiaVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_txGiaVetoableChange
	private void lapHoaDonNhapKho() {
		if (tbModel.getRowCount() <= 0) {
			JOptionPane.showMessageDialog(this, "Chọn sản phẩm của nhà cung cấp sau đó thêm vào danh sách.", "Thông báo.", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		xoaDSCT();
		int[] d = new int[3];
		String st = txNgay.getText();
		String[] s = st.split("-");
		d[0] = Integer.parseInt(s[0]);
		d[1] = Integer.parseInt(s[1]);
		d[2] = Integer.parseInt(s[2]);

		Date dt = new Date(d[2], d[1] - 1, d[0]);// co 12 thang trong 1 nam thang bat dau  =0
		int mancc = ((NhaCungCap) cbNhaCC.getSelectedItem()).getMaNCC();
		int nv = Var.NV.getMaNV();
		PNhapKho pn = new PNhapKho(0, dt, mancc, nv);

		PNhapKhoDao dao = new PNhapKhoDao();
		try {
			dao.them(pn);
				
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Lỗi không thể thêm (1):");
			return;
		}
		txMaPN.setText(Integer.toString(pn.getSoPNK()));
		for (int i = 0; i < tbModel.getRowCount(); i++) {
			int sp = (Integer) tbModel.getValueAt(i, 1);
			int sl = (Integer) tbModel.getValueAt(i, 3);
			double gi = (Double) tbModel.getValueAt(i, 4);

			CTPNhapKho ct = new CTPNhapKho(pn.getSoPNK(), sp, sl, gi);
			dsCT.add(ct);
		}
		// them chi tiet
		CTPNhapKhoDao daoct = new CTPNhapKhoDao();
		try {
			if (daoct.them(dsCT)) {
				JOptionPane.showMessageDialog(this, "Thêm thành công. ", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Lỗi không thể thêm chi tiết (2):");
			return;

		}
	}

	private void xoaChiTietHoaDon() {
		//xoa tbModel
		txMaPN.setText("");
		int i = tbModel.getRowCount() - 1;
		while (i >= 0) {
			tbModel.removeRow(i);
			i--;
		}
		xoaDSCT();

	}

	private void xoaDSCT() {
		// xoa danh sach chi tiet
		int i = dsCT.size() - 1;
		while (i >= 0) {
			dsCT.remove(i);
			i--;
		}

	}
       private void cbNhaCCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbNhaCCActionPerformed
		// TODO add your handling code here:
		if (load) {

			if (save == true) {
				int kq = JOptionPane.showConfirmDialog(this, "Bạn chưa save có muốn save không?", "Xác nhận save hay không.", JOptionPane.YES_NO_OPTION);
				save = false;
				if (kq == JOptionPane.YES_OPTION) {
					lapHoaDonNhapKho();
				}
			}
			xoaChiTietHoaDon();

			loadCobo2();

		}
       }//GEN-LAST:event_cbNhaCCActionPerformed

       private void cbSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbSanPhamActionPerformed
		// TODO add your handling code here:
		if (cbSanPham.getSelectedItem() != null) {
			SanPham sp = (SanPham) cbSanPham.getSelectedItem();
			txGia.setText(Double.toString(sp.getGiaBanHienHanh()));
		}
       }//GEN-LAST:event_cbSanPhamActionPerformed
	public void DefaultFrom() {
		tbModel = new DefaultTableModel();
		// tao cac cot
		this.tbModel.addColumn("STT");
		this.tbModel.addColumn("Mã sản phẩm");
		this.tbModel.addColumn("Tên sản phẩm");
		this.tbModel.addColumn("Số lượng ");
		this.tbModel.addColumn("Giá nhập");

		dsCT = new ArrayList<CTPNhapKho>();

	}

	private int tonTaiTaiViTri(int MaSP) {
		for (int i = 0; i < tbModel.getRowCount(); i++) {
			int tmp = ((Integer) tbModel.getValueAt(i, 1));
			if (tmp == MaSP) {
				return i;
			}
		}
		return -1;
	}

	private int laySoLuong() {
		try {
			int sl = Integer.parseInt(txSoLuong.getText().trim());
			sl = Math.abs(sl);
			txSoLuong.setText(Integer.toString(sl));
			return sl;
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhập sai sô lượng. Nhập lại", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			txSoLuong.requestFocus();
			return 0;
		}
	}

	private void themSanPhamVaoTbModel() {
		if (cbSanPham.getSelectedItem() != null) {
			CTPNhapKho tmp = new CTPNhapKho();
			SanPham sp = new SanPham((SanPham) cbSanPham.getSelectedItem());
			tmp.setMaSP(sp.getMaSP());
			int vt = tonTaiTaiViTri(sp.getMaSP());
			if (vt == -1) {
				int sl = laySoLuong();
				if (sl == 0) {
					return;
				}
				tmp.setSoLuong(sl);
				double gi = 0.0;
				try {
					gi = Double.parseDouble(txGia.getText().trim());
					gi = Math.abs(gi);
					txGia.setText(Double.toString(gi));
				} catch (Exception e) {
					JOptionPane.showMessageDialog(this, "Nhập sai giá. Nhập lại", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
					txGia.requestFocus();
					return;
				}

				tmp.setGiaNhap(gi);
				tmp.setSoPNK(0);
				//	dsCT.add(tmp);
				Object[] items = new Object[]{0,
					tmp.getMaSP(),
					sp.getTenSP(),
					tmp.getSoLuong(),
					tmp.getGiaNhap(),
					tmp.getSoPNK()
				};

				tbModel.addRow(items);
				//}
			} else {// tim ton tai va tang len
				Integer ob = (Integer) tbModel.getValueAt(vt, 3);
				ob += laySoLuong();
				Object o = ob;
				tbModel.setValueAt(o, vt, 3);
			}
			for (int i = 0; i < tbModel.getRowCount(); i++)// cap nhat STT
			{
				Object o = i + 1;
				tbModel.setValueAt(o, i, 0);
			}
		}
	}

	private void xoaKhoiTbModel() {
		int dong = tbChiTiet.getSelectedRow();
		if (dong >= 0 && dong < tbModel.getRowCount()) {
			tbModel.removeRow(dong);
		} else {
			JOptionPane.showMessageDialog(this, "Chọn dòng cần xóa", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);

		}
	}
       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		save = true;
		themSanPhamVaoTbModel();
		tbChiTiet.setModel(tbModel);
		// Canh deu cac dong theo ti le % 
		// co tong cong 6 cot
		int[] va = {10, 10, 40, 30, 30};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = tbChiTiet.getWidth() * va[l] / 100;
			tbChiTiet.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
       }//GEN-LAST:event_jButton1ActionPerformed

       private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
		// TODO add your handling code here:
		xoaKhoiTbModel();
		if (tbModel.getRowCount() == 0) {
			save = false;
		}
		tbChiTiet.setModel(tbModel);


       }//GEN-LAST:event_jButton3ActionPerformed

       private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
		// TODO add your handling code here:
		save = false;

		lapHoaDonNhapKho();
       }//GEN-LAST:event_jButton2ActionPerformed

       private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

		// TODO add your handling code here:save = false;
		save = false;
		xoaChiTietHoaDon();
       }//GEN-LAST:event_jButton4ActionPerformed

       private void txTenNVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txTenNVActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txTenNVActionPerformed
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JComboBox cbNhaCC;
       private javax.swing.JComboBox cbSanPham;
       private javax.swing.JButton jButton1;
       private javax.swing.JButton jButton2;
       private javax.swing.JButton jButton3;
       private javax.swing.JButton jButton4;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JLabel jLabel6;
       private javax.swing.JLabel jLabel7;
       private javax.swing.JLabel jLabel8;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JTable tbChiTiet;
       private javax.swing.JTextField txGia;
       private javax.swing.JTextField txMaPN;
       private javax.swing.JTextField txNgay;
       private javax.swing.JTextField txSoLuong;
       private javax.swing.JTextField txTenNV;
       // End of variables declaration//GEN-END:variables
}
